package com.frxuexi.park2304.news

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.NewsBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ActivityNewsUpdateBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NewsUpdateActivity")
@AndroidEntryPoint
class NewsUpdateActivity : BaseActivity<ActivityNewsUpdateBinding,NewsIntent,NewsViewModel>() {
    override val binding: ActivityNewsUpdateBinding by lazy { ActivityNewsUpdateBinding.inflate(layoutInflater) }
    override val vm: NewsViewModel by viewModels<NewsViewModel>()

    val newsImgAdapter by lazy { NewsImgAdapter() }
    @JvmField
    @Autowired
    var id:Int=-1



    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val bean = it.data as NewsBeanItem
                                binding.bean=bean
                                newsImgAdapter.addData(bean.images)
                                newsImgAdapter.notifyDataSetChanged()
                            }
                            RequestType.UPLOAD->{
                                val date = it.data as String
                                val name = date.split(":")[1]
                                newsImgAdapter.data.add("http://10.161.9.80:2209/images/news/${
                                    SPUtils.getInstance().getInt(
                                        Const.UID)}/${name}")
                                newsImgAdapter.notifyDataSetChanged()
                            }
                            RequestType.UPDATE->{
                                ToastUtils.showLong("修改成功")
                                finish()
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(NewsIntent.getNewsById(id))

        binding.newsUpRv.apply {
            adapter=newsImgAdapter
            layoutManager=GridLayoutManager(this@NewsUpdateActivity,4)
        }

        binding.newsupIv.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.headAdd.setOnClickListener {
            binding.bean?.content=binding.newupContent.text.toString()
            binding.bean?.type=binding.newupType.text.toString()
            binding.bean?.title=binding.newupTitle.text.toString()
            binding.bean?.publisher=binding.newupUser.text.toString()
            binding.bean?.publishDate=binding.newupDate.text.toString()
            binding.bean?.images=newsImgAdapter.data
            vm.sendIntent(NewsIntent.updateNews(id,binding.bean!!))
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode== PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            val path = bean?.path!!


            val split = path.split("/")
            val name = split.get(split.lastIndex)

            vm.sendIntent(NewsIntent.upload(name,path))
        }
    }

}